﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class Delegate_MyTest01 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MyDelegate.Robot robot = new MyDelegate.Robot();
        MyDelegate.RobotBehaviors behavior = new MyDelegate.RobotBehaviors();
        //robot.Instruction += behavior.toWalk(2);      //  因为报错，注释
        robot.Execution();
    }

}

public partial class MyDelegate
{
    public class Robot
    {
        public delegate string InstructHandler(int param);
        public event InstructHandler Instruction;

        public string Execution()
        {
            StringBuilder sb = new StringBuilder();
            if (Instruction != null)
            {
                sb.Append(Instruction + "<br>");
            }
            else
            {
                sb.Append("None instructions for me now." + "<br>");
            }
            return sb.ToString();
        }
    }

    public class RobotBehaviors
    {
        public string toWalk(int steps)
        {
            StringBuilder sb = new StringBuilder();
            if (steps > 0 && steps < 10)
            {
                sb.Append("Hi master, I am going to walk... ");
                sb.Append(string.Format("<font color=red>step{0}</font> from here.", steps));
            }
            return sb.ToString();
        }

    }

}

